﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace gpsmid_wince {
	public partial class frMain : Form {
		private Projection cProj;
		private MapLayer layer, layerxml;

		public frMain() {
			//cProj = new Projection3D(2f-5,1f-5);
			cProj = new Projection2D();
			InitializeComponent();
			
			cProj.setCenter( 53.34729f/180*(float)Math.PI, 83.78229f/180*(float)Math.PI);
			cProj.setScalePPM(1f);			
			frMain_Resize(this, null);
			MapLayer.cProj = cProj;			
#if(DEBUG && !PocketPC)
			layer = new MapLayerDat("");			
			//gpsmid_wince.util.Logger.getInstance("frMain", 100).debug("setting center: "+((MapLayerDat)layer).tl.centerLat);
			//cProj.setCenter( ((MapLayerDat)layer).tl.centerLat, ((MapLayerDat)layer).tl.centerLon);
			//layerxml = new MapLayerXml(@"E:\TMP\map.xml");
#else
			DialogResult dr = od.ShowDialog();
			if (dr == DialogResult.OK) {	
				layer = new MapLayerDat(od.FileName);
			} else { throw new Exception("app canceled"); }
			
#endif
		}

		Pen pBlack = new Pen(Color.Black);
		private void frMain_Paint(object sender, PaintEventArgs e) 	{
			if (layer==null)return;
			Point cent = cProj.toWin(cProj.getCenterLat(), cProj.getCenterLon() );
			Graphics g = e.Graphics;
			
			//Text = "X/Y:"+cent.X+"/"+cent.Y;
			//layerxml.render(g);
			layer.render(g);
			g.DrawEllipse(pBlack, cent.X-2, cent.Y-2, 4,4);
			//g.DrawEllipse(pBlack, 100,100,20,20);
#if !PocketPC && DEBUG
			int[] vv=new int[4];
			cProj.getViewport(vv);
			g.DrawRectangle(pBlack, vv[0], vv[1], vv[2], vv[3]);
#endif
		}

		private void frMain_Resize(object sender, EventArgs e) {
#if !PocketPC && DEBUG
			cProj.setViewport(Width/2-240/2, Height/2-320/2, 240,320);
#else
			cProj.setViewport(Width, Height);
#endif
			Invalidate();
		}

		private void miExit_Click(object sender, EventArgs e) {
			Application.Exit();
		}

		private void tbMain_ButtonClick(object sender, ToolBarButtonClickEventArgs e) {
			if (e.Button == btZoomIn) cProj.setScalePPR(cProj.getScalePPR()*1.5f);else
			if (e.Button == btZoomOut) cProj.setScalePPR(cProj.getScalePPR()/1.5f);
			Invalidate();
		}

		private int lx,ly;
		private int down;
		private void frMain_MouseDown(object sender, MouseEventArgs e) {
			lx = e.X;
			ly = e.Y;
		}

		private void frMain_MouseUp(object sender, MouseEventArgs e) {
			float nlat, nlon;
			cProj.toReal(e.X, e.Y, out nlat, out nlon);
			cProj.setCenter(nlat, nlon);
			Invalidate();
		}
	}
}